package com.ling.controller;

import com.ling.pojo.Admin;
import com.ling.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/admin")
@Controller
public class AdminController {

    @Resource
    private AdminService service;
    public void setService(AdminService service) {
        this.service = service;
    }

    @RequestMapping(value = "/login")
    public String adminLogin() {
        return "/login";
    }

    @RequestMapping(value = "/tologin")
    public String adminLogin(@RequestParam String user, @RequestParam String pwd,
                             HttpServletRequest request, HttpSession session) {
        System.out.println("11111111");
        Admin admin = service.adminLogin(user, pwd);
        if (admin != null) {
            System.out.println("22222222222");
            session.setAttribute("username",user);
            return "redirect:/book/allBook";
        }else {
            request.setAttribute("msg","用户名或密码不正确！");
            return "login";
        }
    }

    @RequestMapping("quit")
    public String quit(HttpServletRequest request,HttpSession session) {
        session.removeAttribute("username");
        session.setAttribute("msg","退出成功，请重新登录！");
        return "login";
    }


}
